// 考试表
class Exam {
  async getExamList(keyword,studentID) {
    let obj1 = global._db('exam').where({ studentId: studentID});
    if (keyword != "" && keyword != undefined) {
      obj1 = obj1.where('name','like','%' + keyword + '%')
    }
    let list = []
    let arr1 = await obj1.where({end:0}).orderBy('startTime','desc')
    const list1 = arr1.map(item => {
      const { id,name,addr,seat,startTime, endTime,end,etype } = item;
      let time = getTimeStrByRange(startTime,endTime)
      list.push({id,name,addr,seat,time,startTime,endTime,end,etype})
    });
    let obj2 = global._db('exam').where({ studentId: studentID});
    if (keyword != "" && keyword != undefined) {
      obj2 = obj2.where('name','like','%' + keyword + '%')
    }
    let arr2 = await obj2.where({end:1}).orderBy('startTime','desc')
    const list2 = arr2.map(item => {
      const { id,name,addr,seat,startTime, endTime,end,etype } = item;
      let time = getTimeStrByRange(startTime,endTime)
      list.push({id,name,addr,seat,time,startTime,endTime,end,etype})
    });
    return list
  }
  async addExam(exam,studentID) {
    if (exam.name === undefined || exam.addr === undefined || exam.seat === undefined) {
      return { code: -1, message: '请输入补全参数' };
    }
    if (exam.name.length > 20) {
      return { code: -1, message: '科目名称20个字符之内' };
    }
    if (exam.addr.length > 20) {
      return { code: -1, message: '考场地址20个字符之内' };
    }
    if (exam.seat.length > 8) {
      return { code: -1, message: '座位信息8个字符之内' };
    }
    if (exam.startTime > exam.endTime || exam.startTime < 0 || exam.endTime < 0) {
      return { code: -1, message: '输入时间错误' };
    }
    exam.etype = 1
    exam.end = 0
    exam.studentId = studentID
    let result = await global._db('exam').insert(exam)
    if (result === 0) {
      return { code: -1, message: '新增考试错误' };
    }
    return { code: 0, message: '新增考试成功' };
  }
  async removeExam(id) {
    await global._db('exam').where({id:id}).del()
    return
  }
  async getMyAchievement(keyword,studentID) {
    let obj = global._db('achievement').where({ studentId: studentID});
    if (keyword != "" && keyword != undefined) {
      obj = obj.where('name','like','%' + keyword + '%')
    }
    const dataList = await obj
    return dataList
  }
}

function getTimeStrByRange(startTime,endTime) {
  let startTimeStr = parseTime(startTime*1000);
  let endTimeStr = parseTime(endTime*1000);
  let str = startTimeStr.slice(6,16) + "-" + endTimeStr.slice(11,16)
  return str
}

function parseTime(timestamp){
  var date = new Date(timestamp);//时间戳为10位需*1000，时间戳为13位的话不需乘1000
  var Y = date.getFullYear() + '-';
  var M = (date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1) + '-';
  var D = (date.getDate() < 10 ? '0'+date.getDate() : date.getDate()) + ' ';
  var h = (date.getHours() < 10 ? '0'+date.getHours() : date.getHours()) + ':';
  var m = (date.getMinutes() < 10 ? '0'+date.getMinutes() : date.getMinutes()) + ':';
  var s = (date.getSeconds() < 10 ? '0'+date.getSeconds() : date.getSeconds());

  let strDate = Y+M+D+h+m+s;
  console.log(strDate) //2020-05-08 17:44:56　
  return strDate;
}

const exam = new Exam();

module.exports = exam;